<!DOCTYPE html>
<html class="iframe-h">

	<head>
		<meta charset="UTF-8">
		<meta name="renderer" content="webkit">
		<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
		<meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />
		<title>转手商城后台-商品分类</title>
		<link rel="stylesheet" type="text/css" href="../../static/layui/css/layui.css" />
		<link rel="stylesheet" type="text/css" href="../../static/css/admin.css" />

		<script src="../../static/js/public/jquery.min.js" type="text/javascript" charset="utf-8"></script>
		<script src="../../static/js/public/jquery.session.js" type="text/javascript" charset="utf-8"></script>
		<script src="../../static/js/public/vue.min.js" type="text/javascript" charset="utf-8"></script>
		<script src="../../static/js/public/axios.min.js" type="text/javascript" charset="utf-8"></script>
		<script src="../../static/js/public/util.js" type="text/javascript" charset="utf-8"></script>

		<script src="../../static/layui/layui.js" type="text/javascript" charset="utf-8"></script>
		<script src="../../static/js/common.children.js" type="text/javascript" charset="utf-8"></script>
	</head>

	<body>
		<div class="wrap-container clearfix" id="list">
			<div class="column-content-detail">
				<form class="layui-form" @submit.prevent="onSubmit">
					<div class="layui-form-item">
						<div class="layui-inline tool-btn">
							<button class="layui-btn layui-btn-small layui-btn-normal addBtn" data-url="product/product-type-add.html"><i
								 class="layui-icon">&#xe654;</i></button>
						</div>
						<div class="layui-inline">
							<input type="text" name="text" placeholder="请输入查询内容" autocomplete="off" class="layui-input">
						</div>
						<button class="layui-btn layui-btn-normal search" lay-submit="search">搜索</button>
					</div>
				</form>
				<div class="layui-form" id="table-list">
					<table class="layui-table" lay-even lay-skin="nob">
						<colgroup>
							<col width="50">
							<col width="150">
							<col width="150">
							<col width="150">
							<col width="100">
						</colgroup>
						<thead>
							<tr>
								<th>ID</th>
								<th>分类名称</th>
								<th>优先级</th>
								<th>启用状态</th>
								<th>操作</th>
							</tr>
						</thead>
						<tbody>
							<tr v-for="(item,index) in obj">
								<td>{{item.ptid}}</td>
								<td>
									<input type="text" v-model="item.name" name="name" lay-verify="required" placeholder="请输入分类名称" autocomplete="off"
									 class="layui-input">
								</td>
								<td>
									<input type="text" v-model="item.priority" name="priority" lay-verify="required" placeholder="请输入优先级(纯数字)"
									 autocomplete="off" class="layui-input">
								</td>
								<td><input type="checkbox" :id="'checkbox_'+item.ptid" :value="item.enableflag" v-model="item.enableflag" name="enableflag"
									 lay-filter="filter" lay-skin="switch"></td>
								<td>
									<div class="layui-inline">
										<button class="layui-btn layui-btn-small layui-btn-normal" data-id="1" @click="ok(item.ptid,index)"><i class="layui-icon">&#xe605;</i></button>
										<button class="layui-btn layui-btn-small layui-btn-danger" data-id="1" @click="del(item.ptid)"><i class="layui-icon">&#xe640;</i></button>
									</div>
								</td>
							</tr>
						</tbody>
					</table>
				</div>
			</div>
		</div>

		<script type="text/javascript">
			layui.use(['form', 'jquery', 'layer'], function() {
				var form = layui.form();
				var jquery = layui.jquery;
				var layer = layui.layer;

				//监听提交
				form.on('switch(filter)', function(data) {
					//console.log(data.elem); //得到checkbox原始DOM对象
					//console.log(data.elem.checked); //开关是否开启，true或者false
					//console.log(data.value); //开关value值，也可以通过data.elem.value得到
					//console.log(data.othis); //得到美化后的DOM对象
					data.elem.value = data.elem.checked
				});
			});

			var vm = new Vue({
				el: '#list',
				data: {
					obj: {}
				},
				methods: {
					loadPage() { //加载记录
						var _this = this;
						axios({
							method: 'get',
							url: getRootURL() + 'admin/select-product-sort',
							headers: {
								'Authorization': $.session.get("token"),
								'Content-Type': 'application/x-www-form-urlencoded'
							}
						}).then(function(res) {
							console.log(res);

							if (res.data.status >= 0) {
								_this.obj = res.data.object
								
								for(var i=0;i<_this.obj.length;i++){
									//取消复选框选中
									
									var id = "#checkbox_"+_this.obj[i].ptid
									var flag = _this.obj[i].enableflag
									
									 layui.use([ 'layer', 'jquery', 'form'], function() {
										 $ = layui.jquery;
										 var form = layui.form();
										 $(id).prop("checked", flag);
										 form.render('checkbox');
									 })
								}
								
								//reload()
								
							} else {
								alert(res.data.msg);
							}

						}).catch(function(err) {
							alert(err);
						});
					},
					del(id) { //删除记录
						var _this = this;
						alert("是否删除记录?", function() {
							axios({
								method: 'post',
								url: getRootURL() + 'admin/delete-product-sort',
								params: {
									ptid: id
								},
								headers: {
									'Authorization': $.session.get("token"),
									'Content-Type': 'application/x-www-form-urlencoded'
								}
							}).then(function(res) {
								console.log(res);

								if (res.data.status >= 0) {
									alert(res.data.msg, function() {
										_this.loadPage();
									});
								} else {
									alert(res.data.msg);
								}

							}).catch(function(err) {
								alert(err);
							});
						});
					},
					ok(id, index) {
						var _this = this;
						alert("是否修改记录?", function() {
							axios({
								method: 'post',
								url: getRootURL() + 'admin/update-product-sort',
								data: {
									ptid: id,
									name: _this.$data.obj[index].name,
									priority: _this.$data.obj[index].priority,
									enableflag: $("#checkbox_" + id).val()
								},
								headers: {
									'Authorization': $.session.get("token"),
									'Content-Type': 'application/json'
								}
							}).then(function(res) {
								console.log(res);

								if (res.data.status >= 0) {
									alert(res.data.msg, function() {
										_this.loadPage();
									});
								} else {
									alert(res.data.msg);
								}

							}).catch(function(err) {
								alert(err);
							});
						});

					},
					onSubmit() {
						//防止form冒泡
						return false;
					}

				},
				created() {
					this.loadPage()
				},
				mounted() {

				},
				updated() {
					layui.use(['form'], function () {
						var form = layui.form();
						form.render();
					});
				}
				
			})
		</script>

	</body>

</html>
